package org.sp.controller;

import org.sp.entity.User;
import org.sp.service.ITestUserService;
import org.sp.service.impl.TestUserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "PutUserController",urlPatterns = "/user/edit")
public class PutUserController extends HttpServlet {
    private ITestUserService service = new TestUserServiceImpl();

    /**
     * 修改
     * @param request
     * @param resp
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String mobile = request.getParameter("mobile");
        String address = request.getParameter("address");
        User user = new User(Integer.parseInt(id),name,mobile,address);
        System.out.println(user);
        Boolean flag = service.updateUser(user);
        if (flag){
            request.getSession().setAttribute("msg","修改成功");
        }else {
            request.getSession().setAttribute("msg","修改失败");
        }
        resp.sendRedirect(request.getContextPath()+"/");
    }

}
